package com.example.refresh;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Map;


public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private ArrayList<String> arrayList = new ArrayList<>();
    private Handler handler;
    private int MSG_REFRESH = 1001;
    private Button btn1;
    private Button btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);


        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("点击了跳转","111111");
                Intent intent = new Intent(MainActivity.this,WaveSwipeRefresh.class);
                startActivity(intent);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,JellyRefresh.class);
                startActivity(intent);
            }
        });

        listView = findViewById(R.id.listview);

        swipeRefreshLayout = findViewById(R.id.swipe_ly);
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MainActivity.this,
                android.R.layout.simple_list_item_1,arrayList);
        listView.setAdapter(arrayAdapter);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what==MSG_REFRESH){
                    swipeRefreshLayout.setRefreshing(false);
                    arrayAdapter.notifyDataSetChanged();
                    return true;
                }
                return false;
            }
        });

        //刷新监听
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i=0;i<14;i++) {
                            arrayList.add("下拉刷新" + i);
                        }
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.sendEmptyMessage(MSG_REFRESH);

                    }
                }).start();
            }
        });

    }
}